package com.managedBean.merchant;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.primefaces.event.SelectEvent;

import com.controller.merchant.RoleController;
import com.dto.merchant.Level1DTO;
import com.dto.merchant.PermissionDTO;
import com.dto.merchant.PermissionDataModel;
import com.dto.merchant.RoleDTO;

@ManagedBean
@ViewScoped
public class RoleManagedBean implements Serializable {

	private static final long serialVersionUID = -5217026628249591184L;
	@ManagedProperty(value = "#{roleController}")
	private RoleController roleController;
	private List<RoleDTO> roleList;
	private RoleDTO selectedRoleDTO;
	private boolean addFlag;
	private boolean editFlag;
	private String roleName;
	private String roleDescription;
	private boolean active;
	private int roleId;
	private List<PermissionDTO> permissionList;
	private PermissionDTO[] selectedPermissions;
	private PermissionDataModel permissionDataModel;
	public RoleManagedBean(){
		selectedRoleDTO=new RoleDTO();
	}
	@PostConstruct
	public void init(){
		roleList=new ArrayList<RoleDTO>();
		roleList=roleController.findAll();
		permissionList=roleController.findAllPermissions();
		permissionDataModel=new PermissionDataModel(permissionList);
	}
	public void addMessage(String summary) {
		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO,
				summary, null);
		FacesContext.getCurrentInstance().addMessage(null, message);
	}
	public void roleSelected(SelectEvent event) {
		FacesMessage msg = new FacesMessage("role Selected",
				((RoleDTO) event.getObject()).getRoleName());
		FacesContext.getCurrentInstance().addMessage(null, msg);
	}
	public void addRole(){
		addFlag=true;
		editFlag=false;
		setRoleName(null);
		setRoleDescription(null);
		setActive(false);
		setRoleId(0);
	}
	public void editRole(){
		addFlag=false;
		editFlag=true;
		setRoleName(selectedRoleDTO.getRoleName());
		setRoleDescription(selectedRoleDTO.getRoleDescription());
		setActive(selectedRoleDTO.isActive());
		setRoleId(selectedRoleDTO.getRoleId());
		selectedPermissions=new PermissionDTO[selectedRoleDTO.getPermissionList().size()];
		for(PermissionDTO perm:selectedRoleDTO.getPermissionList()){
			int i=0;
			selectedPermissions[i]=perm;
			i++;
		}
	}
	public void save(){
		RoleDTO dto =new RoleDTO();
		dto.setRoleName(roleName);
		dto.setActive(active);
		dto.setRoleDescription(roleDescription);
		List<PermissionDTO> list=new ArrayList<PermissionDTO>();
		for(int i=0;i<selectedPermissions.length;i++){
			list.add(selectedPermissions[i]);
		}
		dto.setPermissionList(list);
		if(addFlag){
			roleController.addRole(dto);
		}else{
			dto.setRoleId(roleId);
			roleController.editRole(dto);
		}
		init();
		selectedRoleDTO=null;
		selectedPermissions=new PermissionDTO[0];
		addFlag = false;
		editFlag = false;
		FacesMessage msg = new FacesMessage("Role Added");
		FacesContext.getCurrentInstance().addMessage(null, msg);
	}
	public void cancel() {
		addFlag=false;
		editFlag=false;
		selectedPermissions=new PermissionDTO[0];
	}
	/**
	 * @return the roleController
	 */
	public RoleController getRoleController() {
		return roleController;
	}
	/**
	 * @param roleController the roleController to set
	 */
	public void setRoleController(RoleController roleController) {
		this.roleController = roleController;
	}
	/**
	 * @return the roleList
	 */
	public List<RoleDTO> getRoleList() {
		return roleList;
	}
	/**
	 * @param roleList the roleList to set
	 */
	public void setRoleList(List<RoleDTO> roleList) {
		this.roleList = roleList;
	}
	/**
	 * @return the selectedRoleDTO
	 */
	public RoleDTO getSelectedRoleDTO() {
		return selectedRoleDTO;
	}
	/**
	 * @param selectedRoleDTO the selectedRoleDTO to set
	 */
	public void setSelectedRoleDTO(RoleDTO selectedRoleDTO) {
		this.selectedRoleDTO = selectedRoleDTO;
	}
	/**
	 * @return the displayAddFlag
	 */
	public boolean isaddFlag() {
		return addFlag;
	}
	/**
	 * @param displayAddFlag the displayAddFlag to set
	 */
	public void setaddFlag(boolean addFlag) {
		this.addFlag = addFlag;
	}
	/**
	 * @return the roleName
	 */
	public String getRoleName() {
		return roleName;
	}
	/**
	 * @param roleName the roleName to set
	 */
	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}
	/**
	 * @return the roleDescription
	 */
	public String getRoleDescription() {
		return roleDescription;
	}
	/**
	 * @param roleDescription the roleDescription to set
	 */
	public void setRoleDescription(String roleDescription) {
		this.roleDescription = roleDescription;
	}
	/**
	 * @return the active
	 */
	public boolean isActive() {
		return active;
	}
	/**
	 * @param active the active to set
	 */
	public void setActive(boolean active) {
		this.active = active;
	}
	/**
	 * @return the serialversionuid
	 */
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	/**
	 * @return the addFlag
	 */
	public boolean isAddFlag() {
		return addFlag;
	}
	/**
	 * @param addFlag the addFlag to set
	 */
	public void setAddFlag(boolean addFlag) {
		this.addFlag = addFlag;
	}
	/**
	 * @return the editflag
	 */
	public boolean isEditflag() {
		return editFlag;
	}
	/**
	 * @param editflag the editflag to set
	 */
	public void setEditflag(boolean editFlag) {
		this.editFlag = editFlag;
	}
	/**
	 * @return the roleId
	 */
	public int getRoleId() {
		return roleId;
	}
	/**
	 * @param roleId the roleId to set
	 */
	public void setRoleId(int roleId) {
		this.roleId = roleId;
	}
	/**
	 * @return the editFlag
	 */
	public boolean isEditFlag() {
		return editFlag;
	}
	/**
	 * @param editFlag the editFlag to set
	 */
	public void setEditFlag(boolean editFlag) {
		this.editFlag = editFlag;
	}
	/**
	 * @return the permissionList
	 */
	public List<PermissionDTO> getPermissionList() {
		return permissionList;
	}
	/**
	 * @param permissionList the permissionList to set
	 */
	public void setPermissionList(List<PermissionDTO> permissionList) {
		this.permissionList = permissionList;
	}
	/**
	 * @return the selectedPermissions
	 */
	public PermissionDTO[] getSelectedPermissions() {
		return selectedPermissions;
	}
	/**
	 * @param selectedPermissions the selectedPermissions to set
	 */
	public void setSelectedPermissions(PermissionDTO[] selectedPermissions) {
		this.selectedPermissions = selectedPermissions;
	}
	/**
	 * @return the permissionDataModel
	 */
	public PermissionDataModel getPermissionDataModel() {
		return permissionDataModel;
	}
	/**
	 * @param permissionDataModel the permissionDataModel to set
	 */
	public void setPermissionDataModel(PermissionDataModel permissionDataModel) {
		this.permissionDataModel = permissionDataModel;
	}
}
